package com.ksd.web.controller.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ksd.common.config.PlayControlConfig;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.utils.StringUtils;
import com.ksd.framework.web.domain.server.Sys;
import com.ksd.quartz.util.TaskConstans;
import com.ksd.system.domain.SysBoard;
import com.ksd.system.domain.SysChannel;
import com.ksd.system.domain.SysChannelTemplate;
import com.ksd.system.service.*;
import com.ksd.web.controller.m3beans.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class OutputChannelService {
    public final Logger logger = LoggerFactory.getLogger(OutputChannelService.class);
    @Autowired
    private ISysOperLogService sysOperLogService;
    @Autowired
    private SendChannelService sendChannelService;
    @Autowired
    private ISysChannelTemplateService sysChannelTemplateService;
    @Autowired
    private ISysChannelService sysChannelService;

    public int setMainFreqEx(SysBoard sysBoard, DeliveryParamObject deliveryParamObject) {
        String bordId = deliveryParamObject.getBordId();
        List<DeliveryParam> deliveryParam = deliveryParamObject.getDeliveryParam();
//        System.out.println("===deliveryParam size======"+deliveryParam.size());
        Integer deliveryType = 10;
        MainFreqParameters mainFreqParameters = new MainFreqParameters();
        List<FreqInfo> freqs = new ArrayList<>();
        FreqInfo freqInfo = new FreqInfo();
        freqInfo.setDeliveryType(deliveryType);
        freqInfo.setDeliveryParams(deliveryParam);
//        System.out.println("===setDeliveryParams size===="+freqInfo.getDeliveryParams().size());
        freqs.add(freqInfo);
        mainFreqParameters.setFreqInfo(freqs);
        String serviceUrl = sysBoard.getUrl();
        String response = sendChannelService.setMainFreqEx(serviceUrl, mainFreqParameters);
        if (StringUtils.isEmpty(response)) {
            return -1;
        }
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // 将 JSON 字符串解析为 JsonNode 对象
            JsonNode rootNode = objectMapper.readTree(response);
            // 获取 status 节点的值
            int statusInt = rootNode.get("status").asInt();
            if (statusInt == 0){
                return 0;
            }
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
        return -1;
    };

    public AjaxResult setPrfDataSave(SysBoard sysBoard, SaveSatelliteInfo saveSatelliteInfo) {
//        System.out.println("setPrfDataSave: "+saveSatelliteInfo.toString());
        String satellite = saveSatelliteInfo.getSatellite(); //1表示高频头1  2表示高频头2
        String board = saveSatelliteInfo.getBoard();
        String serviceUrl = "";

        Integer checkSelectTatolNumber = 0;
        String checkBoardId = "";
        if (board.equals(TaskConstans.boardValue1)){
            //当前配置的是直播板卡1的频道
            checkBoardId = TaskConstans.boardValue2;
        }else if(board.equals(TaskConstans.boardValue2)){
            //当前配置的是直播板卡2的频道
            checkBoardId = TaskConstans.boardValue1;
        }
        SysChannelTemplate checkSelect = new SysChannelTemplate();
        checkSelect.setStatus(TaskConstans.status_selected);
//        checkSelect1.setBoardId(Long.valueOf(TaskConstans.boardValue1));
        checkSelect.setBoardId(Long.valueOf(checkBoardId));
        List<SysChannelTemplate> checkOtherSelectList = sysChannelTemplateService.selectSysChannelTemplateList(checkSelect);

//        SysChannelTemplate checkSelect2 = new SysChannelTemplate();
//        checkSelect2.setStatus(TaskConstans.status_selected);
//        checkSelect2.setBoardId(Long.valueOf(TaskConstans.boardValue2));
//        List<SysChannelTemplate> checkSelectList2 = sysChannelTemplateService.selectSysChannelTemplateList(checkSelect2);

        Set<String> selectNames = new HashSet<>();//校验选中的频道是否有重复
        Set<String> selectIps = new HashSet<>(); //校验选中的频道分配的ip是否有重复

        serviceUrl = sysBoard.getUrl();

        List<SaveServiceInfo> serviceInfo = saveSatelliteInfo.getServiceInfo();

        //校验频道是否有重复选择
//        System.out.println("校验选择保存频道是否有重复");
        Set<String> names = new HashSet<>();  //存储其他板卡已配置的名称,后续需要判断是否有重复
        Set<String> ips = new HashSet<>();  //存储其他板卡已配置的IP,后续需要判断是否有重复
        for (SysChannelTemplate otherSelectBoard : checkOtherSelectList) {
            //校验板卡1是否有已经重复选中的栏目
            String name = otherSelectBoard.getName();
            String ip = otherSelectBoard.getIp();
            names.add(name);
            ips.add(ip);
//            System.out.println("另一个直播板卡的频道,即从数据库中查询到的直播板卡2的频道名称:"+name);
        }

//        System.out.println("获取另一个直播板卡保存的直播频道数:"+names.size());
        SaveServiceInfo saveServiceInfo1 = serviceInfo.get(0);
        if (StringUtils.isNotNull(saveServiceInfo1)){
            List<SaveService> saveServices = saveServiceInfo1.getServiceinfo();
            checkSelectTatolNumber += saveServices.size();
//            System.out.println("校验高频头1选择分发默认频道的个数:"+saveServices.size());
            for (SaveService saveService : saveServices) {
                String name = saveService.getName();
                String ip = saveService.getIp();
//                System.out.println(name+"============"+ip);
                if (!selectNames.add(name)){
                    //校验当前直播板卡的高频头1和高频头选择配置的频道是否有重复
                    CheckChannelBean checkChannelBean = new CheckChannelBean();
                    checkChannelBean.setFlag(false);
                    checkChannelBean.setMessage("保存失败:当前直播板卡重复选择分发频道: "+name );
                    return AjaxResult.error(checkChannelBean);
                }
                if(!selectIps.add(ip)){
                    //校验当前直播板卡的高频头1和高频头选择配置的频道是否有重复
                    CheckChannelBean checkChannelBean = new CheckChannelBean();
                    checkChannelBean.setFlag(false);
                    checkChannelBean.setMessage("保存失败:当前选择的频道: "+name+" ,分配的输出地址: "+ip+" 有重复,建议重新修改该输出地址");
                    return AjaxResult.error(checkChannelBean);
                }
                if (!names.add(name)){
                    CheckChannelBean checkChannelBean = new CheckChannelBean();
                    checkChannelBean.setFlag(false);
                    checkChannelBean.setMessage("保存失败:当前选择的频道: "+name+" 在直播板卡2的高频头1已分发");
                    return AjaxResult.error(checkChannelBean);
                }
                if(!ips.add(ip)){
                    CheckChannelBean checkChannelBean = new CheckChannelBean();
                    checkChannelBean.setFlag(false);
                    checkChannelBean.setMessage("保存失败:当前选择的频道: "+name+" 的输出地址: "+ip+" ,在直播板卡2的高频头1已分发,建议手动修改该输出地址");
                    return AjaxResult.error(checkChannelBean);
                }
            }
        }
        SaveServiceInfo saveServiceInfo2 = serviceInfo.get(1);
        if (StringUtils.isNotNull(saveServiceInfo2)){
            List<SaveService> saveServices = saveServiceInfo2.getServiceinfo();
            checkSelectTatolNumber += saveServices.size();
//            System.out.println("校验高频头2选择分发默认频道的个数:"+saveServices.size());
            for (SaveService saveService : saveServices) {
                String name = saveService.getName();
                String ip = saveService.getIp();
//                System.out.println("name:"+name+"========ip:"+ip);
                if (!selectNames.add(name)){
                    //校验当前直播板卡的高频头1和高频头选择配置的频道是否有重复
                    CheckChannelBean checkChannelBean = new CheckChannelBean();
                    checkChannelBean.setFlag(false);
                    checkChannelBean.setMessage("保存失败:当前直播板卡重复选择分发频道4444: "+name);
                    return AjaxResult.error(checkChannelBean);
                }
                if(!selectIps.add(ip)){
                    //校验当前直播板卡的高频头1和高频头选择配置的频道是否有重复
                    CheckChannelBean checkChannelBean = new CheckChannelBean();
                    checkChannelBean.setFlag(false);
                    checkChannelBean.setMessage("保存失败:当前选择的频道: "+name+" ,分配的输出地址: "+ip+" 有重复,建议重新修改该输出地址");
                    return AjaxResult.error(checkChannelBean);
                }
                if (!names.add(name)){
                    CheckChannelBean checkChannelBean = new CheckChannelBean();
                    checkChannelBean.setFlag(false);
                    checkChannelBean.setMessage("保存失败:当前选择的频道: "+name+" 在直播板卡2的高频头2已分发");
                    return AjaxResult.error(checkChannelBean);
                }
                if(!ips.add(ip)){
                    CheckChannelBean checkChannelBean = new CheckChannelBean();
                    checkChannelBean.setFlag(false);
                    checkChannelBean.setMessage("保存失败:当前选择的频道: "+name+" 的输出地址: "+ip+" ,在直播板卡2的高频头2已分发,建议手动修改该输出地址");
                    return AjaxResult.error(checkChannelBean);
                }
            }
        }

//        System.out.println("=============checkSelectTatolNumber:"+checkSelectTatolNumber+"=======================显示每次分发频道个数:"+ PlayControlConfig.getSelectSysChannelListCount());
        if (checkSelectTatolNumber > Integer.valueOf(PlayControlConfig.getSelectSysChannelListCount())){
            CheckChannelBean checkChannelBean = new CheckChannelBean();
            checkChannelBean.setFlag(false);
            checkChannelBean.setMessage("保存失败:每次选择配置的频道个数不允许超过16个 ,当前选择了"+checkSelectTatolNumber+"个频道");
            return AjaxResult.error(checkChannelBean);
        }

        String response = sendChannelService.setPrfData(serviceInfo,serviceUrl,board);
        sysOperLogService.insertOperlogSuccess("频道输出接口.请求地址: "+serviceUrl+",响应数据:"+response);
        if (StringUtils.isEmpty(response)){
            sysOperLogService.insertOperlogApiError("频道输出设置接口.请求地址: "+serviceUrl+",响应超时,请检查网络连接或联系管理员稍后重试");
            return AjaxResult.error("频道输出设置接口响应超时,请检查网络连接或联系管理员稍后重试");
        }
        ObjectMapper mapperPage = new ObjectMapper();
        GetPrfDataResponse getPrfDataResponse = null;


        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // 将 JSON 字符串解析为 JsonNode 对象
            JsonNode rootNode = objectMapper.readTree(response);
            // 获取 status 节点的值
            int statusInt = rootNode.get("status").asInt();
            //先状态清空
            List<SysChannel> sysChannels = new ArrayList<>();
            if (TaskConstans.boardValue1.equals(board)){
                sysChannels = sysChannelService.selectSysChannelListByOrStatus(TaskConstans.status_selected, TaskConstans.status_selected, SysChannel.table_one);
            }else if (TaskConstans.boardValue2.equals(board)){
                sysChannels = sysChannelService.selectSysChannelListByOrStatus(TaskConstans.status_selected, TaskConstans.status_selected, SysChannel.table_twice);
            }
            for (SysChannel sysChannel : sysChannels) {
                sysChannel.setStatus1(TaskConstans.status_init);
                sysChannel.setStatus2(TaskConstans.status_init);
                if (TaskConstans.boardValue1.equals(board)){
                    sysChannelService.updateSysChannel(sysChannel,SysChannel.table_one);
                }else if (TaskConstans.boardValue2.equals(board)){
                    sysChannelService.updateSysChannel(sysChannel,SysChannel.table_twice);
                }
                SysChannelTemplate sysChannelTemplate = sysChannelTemplateService.selectSysChannelTemplateByName(sysChannel.getName());
                if(StringUtils.isNotNull(sysChannelTemplate)){
                    sysChannelTemplate.setStatus(TaskConstans.status_init);
                    sysChannelTemplateService.updateSysChannelTemplate(sysChannelTemplate);
                }
            }
            if (statusInt != 0) {
                sysOperLogService.insertOperlogApiError("频道输出设置接口.请求地址: "+serviceUrl+",响应状态失败，请联系管理员检查接口服务或稍后重试");
                return AjaxResult.error("频道输出设置接口响应状态失败，请联系管理员检查接口服务或稍后重试");
            }else{
                //先一律将上一次选中的频道状态恢复为初始状态,再将这次选中的状态更新
                if (StringUtils.isNotNull(getPrfDataResponse)){
                    List<GetPrfDataObj> data = getPrfDataResponse.getData();
                    if (StringUtils.isNotNull(data)){
                        GetPrfDataObj dataObj1 = data.get(0);
                        GetPrfDataObj dataObj2 = data.get(1);
                        List<ServiceInfo> serviceinfo1 = dataObj1.getServiceinfo();
                        for (ServiceInfo info : serviceinfo1) {
                            String name = info.getName();
                            SysChannel temp = new SysChannel();
                            temp.setName(name);
                            temp.setStatus1(TaskConstans.status_selected);
                            if (TaskConstans.boardValue1.equals(board)){
                                sysChannelService.updateSysChannel(temp,SysChannel.table_one);
                            }else if (TaskConstans.boardValue2.equals(board)){
                                sysChannelService.updateSysChannel(temp,SysChannel.table_twice);
                            }
                            SysChannelTemplate sysChannelTemplate = sysChannelTemplateService.selectSysChannelTemplateByName(temp.getName());
                            if (StringUtils.isNotNull(sysChannelTemplate)) {
                                sysChannelTemplate.setIp(temp.getIp());
                                sysChannelTemplate.setPort(temp.getPort());
                                sysChannelTemplate.setStatus(TaskConstans.status_selected);
                                sysChannelTemplate.setBoardId(Long.valueOf(board));
                                sysChannelTemplate.setFreq(temp.getFreq());
                                sysChannelTemplateService.updateSysChannelTemplate(sysChannelTemplate);
                            }else{
                                sysChannelTemplate = new SysChannelTemplate();
                                sysChannelTemplate.setIp(temp.getIp());
                                sysChannelTemplate.setPort(temp.getPort());
                                sysChannelTemplate.setStatus(TaskConstans.status_selected);
                                sysChannelTemplate.setBoardId(Long.valueOf(board));
                                sysChannelTemplate.setName(temp.getName());
                                sysChannelTemplate.setFreq(temp.getFreq());

                                List<SysChannelTemplate> sysChannelTemplates = sysChannelTemplateService.selectSysChannelTemplateListByChannelId(new SysChannelTemplate());
                                sysChannelTemplate.setChannelId(sysChannelTemplates.get(sysChannelTemplates.size()-1).getChannelId()+1);
                                sysChannelTemplateService.insertSysChannelTemplate(sysChannelTemplate);
                            }
                        }
                        List<ServiceInfo> serviceinfo = dataObj2.getServiceinfo();
                        for (ServiceInfo info : serviceinfo) {
                            String name = info.getName();
                            SysChannel temp = new SysChannel();
                            temp.setName(name);
                            temp.setStatus2(TaskConstans.status_selected);
                            if (TaskConstans.boardValue1.equals(board)){
                                sysChannelService.updateSysChannel(temp,SysChannel.table_one);
                            }else if (TaskConstans.boardValue2.equals(board)){
                                sysChannelService.updateSysChannel(temp,SysChannel.table_twice);
                            }

                            SysChannelTemplate sysChannelTemplate = sysChannelTemplateService.selectSysChannelTemplateByName(temp.getName());
                            if (StringUtils.isNotNull(sysChannelTemplate)) {
                                sysChannelTemplate.setIp(temp.getIp());
                                sysChannelTemplate.setPort(temp.getPort());
                                sysChannelTemplate.setStatus(TaskConstans.status_selected);
                                sysChannelTemplate.setBoardId(Long.valueOf(board));
                                sysChannelTemplate.setFreq(temp.getFreq());

                                sysChannelTemplateService.updateSysChannelTemplate(sysChannelTemplate);
                            }else{
                                sysChannelTemplate = new SysChannelTemplate();
                                sysChannelTemplate.setIp(temp.getIp());
                                sysChannelTemplate.setPort(temp.getPort());
                                sysChannelTemplate.setStatus(TaskConstans.status_selected);
                                sysChannelTemplate.setBoardId(Long.valueOf(board));
                                sysChannelTemplate.setName(temp.getName());
                                sysChannelTemplate.setFreq(temp.getFreq());

                                List<SysChannelTemplate> sysChannelTemplates = sysChannelTemplateService.selectSysChannelTemplateListByChannelId(new SysChannelTemplate());
                                sysChannelTemplate.setChannelId(sysChannelTemplates.get(sysChannelTemplates.size()-1).getChannelId()+1);
                                sysChannelTemplateService.insertSysChannelTemplate(sysChannelTemplate);
                            }
                        }
                    }
                }
                CheckChannelBean checkChannelBean = new CheckChannelBean();
                checkChannelBean.setFlag(true);
                return AjaxResult.success(checkChannelBean);
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public int getPrfDataList(SysBoard sysBoard,QueryInfo queryInfo){
        GetPrfDataResponse getPrfDataResponse = null;
        String board = queryInfo.getBoard();
        String serviceUrl = "";
        serviceUrl = sysBoard.getUrl();
        String response = sendChannelService.getPrfData(serviceUrl);
        sysOperLogService.insertOperlogSuccess("频道管理接口.请求地址: "+serviceUrl+",响应数据:"+response);
        if (StringUtils.isEmpty(response)){
            sysOperLogService.insertOperlogApiError("频道管理接口.请求地址: "+serviceUrl+",响应超时,请联系管理员检查接口服务或稍后重试");
            return -1;
        }
        ObjectMapper mapperPage = new ObjectMapper();
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // 将 JSON 字符串解析为 JsonNode 对象
            JsonNode rootNode = objectMapper.readTree(response);
            // 获取 status 节点的值
            int statusInt = rootNode.get("status").asInt();
            if (statusInt == 2) {
                sysOperLogService.insertOperlogApiError("频道管理接口.请求地址: "+serviceUrl+",响应状态失败，请联系管理员检查接口服务或稍后重试");
                logger.error("频道管理接口响应状态失败，请联系管理员检查接口服务或稍后重试");
            } else if (statusInt == 0) {
                getPrfDataResponse = mapperPage.readValue(response, GetPrfDataResponse.class);
                List<GetPrfDataObj> data = getPrfDataResponse.getData();
                if (StringUtils.isNotNull(data)) {
                    //先将已有的状态初始化
                    if (TaskConstans.boardValue1.equals(board)){
                        List<SysChannel> sysChannelOnes = sysChannelService.selectSysChannelListByOrStatus(TaskConstans.status_selected, TaskConstans.status_selected,SysChannel.table_one);
                        System.out.println("======sysChannelOnesservcice=====数据库中查询到的直播板卡1查询到的个数:"+sysChannelOnes.size());
                        for (SysChannel sysChannelOne : sysChannelOnes) {
                            sysChannelOne.setStatus1(TaskConstans.status_init);
                            sysChannelOne.setStatus2(TaskConstans.status_init);
                            sysChannelService.updateSysChannel(sysChannelOne,SysChannel.table_one);
                        }
                    }else if(TaskConstans.boardValue2.equals(board)){
                        List<SysChannel> sysChannelTwices = sysChannelService.selectSysChannelListByOrStatus(TaskConstans.status_selected, TaskConstans.status_selected,SysChannel.table_twice);
                        System.out.println("======sysChannelOnes=====数据库中查询到的直播板卡2查询到的个数:"+sysChannelTwices.size());
                        for (SysChannel sysChannelOne : sysChannelTwices) {
                            sysChannelOne.setStatus1(TaskConstans.status_init);
                            sysChannelOne.setStatus2(TaskConstans.status_init);
                            sysChannelService.updateSysChannel(sysChannelOne,SysChannel.table_twice);
                        }
                    }
                    SysChannelTemplate template = new SysChannelTemplate();
                    template.setBoardId(Long.valueOf(board));
                    template.setStatus(TaskConstans.status_selected);
                    List<SysChannelTemplate> templatesSelected = sysChannelTemplateService.selectSysChannelTemplateList(template);
                    for (SysChannelTemplate sysChannelTemplate : templatesSelected) {
                        sysChannelTemplate.setStatus(TaskConstans.status_init);
                        sysChannelTemplateService.updateSysChannelTemplate(sysChannelTemplate);
                    }

                    GetPrfDataObj getPrfDataObj1 = data.get(0);
                    GetPrfDataObj getPrfDataObj2 = data.get(1);
                    if (StringUtils.isNotNull(getPrfDataObj1)) {
                        List<ServiceInfo> serviceinfo = getPrfDataObj1.getServiceinfo();
                        if (StringUtils.isNotNull(serviceinfo)){
                            for (ServiceInfo result : serviceinfo) {
                                SysChannel sysChannel = new SysChannel();
                                if (TaskConstans.boardValue1.equals(board)){
                                    sysChannel = sysChannelService.selectSysChannelByName(result.getName(),SysChannel.table_one);
                                }else if (TaskConstans.boardValue2.equals(board)){
                                    sysChannel = sysChannelService.selectSysChannelByName(result.getName(),SysChannel.table_twice);
                                }
                                if (StringUtils.isNotNull(sysChannel)) {
                                    sysChannel.setStatus1(TaskConstans.status_selected);
                                    String url = result.getUrl();
                                    int index1 = url.lastIndexOf("//");
                                    int index2 = url.lastIndexOf(":");
                                    String ip = url.substring(index1 + 2, index2);
                                    String port = url.substring(index2+1, url.length());
                                    sysChannel.setIp(ip);
                                    sysChannel.setPort(port);
//                                    System.out.println("更新高频头1状态: "+sysChannel.getName()+"=========="+board+"=========="+sysChannel.getStatus1());
                                    if (TaskConstans.boardValue1.equals(board)){
                                        sysChannelService.updateSysChannel(sysChannel,SysChannel.table_one);
                                    }else if (TaskConstans.boardValue2.equals(board)){
                                        sysChannelService.updateSysChannel(sysChannel,SysChannel.table_twice);
                                    }
                                    SysChannelTemplate sysChannelTemplate = sysChannelTemplateService.selectSysChannelTemplateByName(result.getName());
                                    if (StringUtils.isNotNull(sysChannelTemplate)) {
                                        sysChannelTemplate.setIp(sysChannel.getIp());
                                        sysChannelTemplate.setPort(sysChannel.getPort());
                                        sysChannelTemplate.setStatus(TaskConstans.status_selected);
                                        sysChannelTemplate.setBoardId(Long.valueOf(board));
                                        sysChannelTemplate.setFreq(sysChannel.getFreq());

                                        sysChannelTemplateService.updateSysChannelTemplate(sysChannelTemplate);
                                    }else{
                                        sysChannelTemplate = new SysChannelTemplate();
                                        sysChannelTemplate.setIp(sysChannel.getIp());
                                        sysChannelTemplate.setPort(sysChannel.getPort());
                                        sysChannelTemplate.setStatus(TaskConstans.status_selected);
                                        sysChannelTemplate.setBoardId(Long.valueOf(board));
                                        sysChannelTemplate.setName(result.getName());
                                        sysChannelTemplate.setFreq(sysChannel.getFreq());

                                        List<SysChannelTemplate> sysChannelTemplates = sysChannelTemplateService.selectSysChannelTemplateListByChannelId(new SysChannelTemplate());
                                        sysChannelTemplate.setChannelId(sysChannelTemplates.get(sysChannelTemplates.size()-1).getChannelId()+1);
                                        sysChannelTemplateService.insertSysChannelTemplate(sysChannelTemplate);
                                    }
                                }
                            }
                        }
                    }
                    if (StringUtils.isNotNull(getPrfDataObj2)) {
                        List<ServiceInfo> serviceinfo = getPrfDataObj2.getServiceinfo();
                        if (StringUtils.isNotNull(serviceinfo)) {
                            for (ServiceInfo result : serviceinfo) {
                                SysChannel sysChannel = new SysChannel();
                                if (TaskConstans.boardValue1.equals(board)){
                                    sysChannel = sysChannelService.selectSysChannelByName(result.getName(),SysChannel.table_one);
                                }else if (TaskConstans.boardValue2.equals(board)){
                                    sysChannel = sysChannelService.selectSysChannelByName(result.getName(),SysChannel.table_twice);
                                }
                                if (StringUtils.isNotNull(sysChannel)) {
                                    sysChannel.setStatus2(TaskConstans.status_selected);
                                    String url = result.getUrl();
                                    int index1 = url.lastIndexOf("//");
                                    int index2 = url.lastIndexOf(":");
                                    String ip = url.substring(index1 + 2, index2);
                                    String port = url.substring(index2+1, url.length());
                                    sysChannel.setIp(ip);
                                    sysChannel.setPort(port);
                                    if (TaskConstans.boardValue1.equals(board)){
                                        sysChannelService.updateSysChannel(sysChannel,SysChannel.table_one);
                                    }else if (TaskConstans.boardValue2.equals(board)){
                                        sysChannelService.updateSysChannel(sysChannel,SysChannel.table_twice);
                                    }
                                    SysChannelTemplate sysChannelTemplate = sysChannelTemplateService.selectSysChannelTemplateByName(result.getName());
                                    if (StringUtils.isNotNull(sysChannelTemplate)) {
                                        sysChannelTemplate.setIp(sysChannel.getIp());
                                        sysChannelTemplate.setPort(sysChannel.getPort());
                                        sysChannelTemplate.setStatus(TaskConstans.status_selected);
                                        sysChannelTemplate.setBoardId(Long.valueOf(board));
                                        sysChannelTemplate.setFreq(sysChannel.getFreq());
                                        sysChannelTemplateService.updateSysChannelTemplate(sysChannelTemplate);
                                    }else{
                                        sysChannelTemplate = new SysChannelTemplate();
                                        sysChannelTemplate.setIp(sysChannel.getIp());
                                        sysChannelTemplate.setPort(sysChannel.getPort());
                                        sysChannelTemplate.setStatus(TaskConstans.status_selected);
                                        sysChannelTemplate.setBoardId(Long.valueOf(board));
                                        sysChannelTemplate.setName(result.getName());
                                        sysChannelTemplate.setFreq(sysChannel.getFreq());

                                        List<SysChannelTemplate> sysChannelTemplates = sysChannelTemplateService.selectSysChannelTemplateListByChannelId(new SysChannelTemplate());
                                        sysChannelTemplate.setChannelId(sysChannelTemplates.get(sysChannelTemplates.size()-1).getChannelId()+1);
                                        sysChannelTemplateService.insertSysChannelTemplate(sysChannelTemplate);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return 0;

    }


}
